package cn.wolfcode.service.impl;

import cn.wolfcode.domain.EmployeeNotice;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.mapper.EmployeeNoticeMapper;
import cn.wolfcode.mapper.NoticeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private EmployeeNoticeMapper employeeNoticeMapper;

    @Override
    public void save(Notice notice) {
        //添加到公告板
        notice.setCreateUser(UserContext.getCurrentUser());
        notice.setCreateTime(new Date());
        notice.setStatus(false);
        noticeMapper.insert(notice);
        //中间表中添加一条信息
        EmployeeNotice employeeNotice = new EmployeeNotice();
        employeeNotice.setRead_status(true);
        employeeNotice.setEmployee_id(UserContext.getCurrentUser().getId());
        employeeNotice.setNotice(notice.getId());
        employeeNoticeMapper.insert(employeeNotice);
    }

    @Override
    public void delete(Long id) {
        //删除中间表中的关联
        employeeNoticeMapper.deleteByPrimaryKey(id);
        //删除公告板的信息
        noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Notice notice) {
        noticeMapper.updateByPrimaryKey(notice);
    }

    @Override
    public Notice get(Long id) {
        return noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Notice> listAll() {
        return noticeMapper.selectAll();
    }

    @Override
    public PageInfo<Notice> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Notice> notices = noticeMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Notice>(notices);
    }



    @Override
    public void updatePublishStatus(Long id) {
        noticeMapper.updatePublishStatus(id);
    }

    @Override
    public List<Notice> adminNoticeById(Long id) {
        return noticeMapper.adminNoticeById(id);
    }

    @Override
    public List<Notice> userNoticeById(Long id) {
        return noticeMapper.userNoticeById(id);
    }
}
